package com.core.framework.web.easyweb.forward;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.core.framework.web.easyweb.EasywebStatics;
import com.core.framework.web.easyweb.context.ServletActionContext;

public class ActionForward implements EasywebStatics 
{
	 /**
	  * 带参数跳转
	  * @param url
	  * @param request
	  * @param response
	  * @throws ServletException
	  * @throws IOException
	  */
	 public static void forward(String url, HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		request.removeAttribute(javax_servlet_include_servlet_path);
		request.removeAttribute(javax_servlet_forward_servlet_path);
		request.setAttribute(javax_servlet_include_servlet_path, url);
		request.setAttribute(javax_servlet_forward_servlet_path, url);
		if (!response.isCommitted())
			request.getRequestDispatcher(url).forward(request, response);
	}

	 /**
	  * 不带参数跳转
	  * @param url
	  * @param response
	  * @throws Exception
	  */
	public static void sendRedirect(String url, HttpServletResponse response)throws Exception 
	{
		ServletActionContext.getRequest().setAttribute(HttpServletResponseIsSend,"TRUE");
		response.sendRedirect(url);
	}
	public  String getPathWithOutExt(HttpServletRequest request) {
		String path = request.getRequestURI();
		if (path.startsWith(request.getContextPath()))
			path = request.getRequestURI().substring(
					request.getContextPath().length());
		if (path.indexOf(".") > 0) {
			path = path.substring(0, path.lastIndexOf("."));
		}
		path = path.replaceAll("/{1}", "/");
		return path;
	}
	public String getPathWithOutContextPath(HttpServletRequest request)
	{
		return getPathWithOutExt(request);
	}
	public  String getActionMethod(HttpServletRequest request) {
		String path = getPathWithOutExt(request);
		if (path.indexOf("!") > 0) {
			path = path.substring(path.lastIndexOf("!") + 1);
		} else {
			return "index";
		}
		return path;
	}
	public  String getActionPath(HttpServletRequest request) {
		String path = this.getPathWithOutExt(request);
		if (path.indexOf("!") > 0) {
			path = path.substring(0, path.lastIndexOf("!"));
		}
		if (path != null)
			path = path.replaceAll("/{2,}", "/");
		return path;
	}
	/**
	 * 获取请求扩展名
	 * @param request
	 * @return
	 */
	public  String getExpandedName(HttpServletRequest request)
	{
		String uri= request.getRequestURI();
		if(uri.indexOf(".")>0)
		{
			String extendedName = uri.substring(uri.indexOf("."));
			return extendedName;
		}
		return expandFileName;
	}
	/**
	 * 补充URL传过来的参数
	 * @param request
	 */
	public void putUrlPageContent(HttpServletRequest request)
	{
		for(Object obj : request.getParameterMap().keySet())
		{
			String value = request.getParameter((String)obj);
			request.setAttribute(obj.toString(), value);
		}
	}
	public static void main(String[] args)
	{
	}
}
